Risk Engine
Overview
The Risk Engine forms the core module to manage risk in the protocol. It is used by all other modules to value assets, fetch ltvs, validate liquidation operations and other risk operations.
The Risk Engine stores important risk data such as oracle addresses and
pool-asset LTVs. It is also closely related to the RiskModule
which stores all
the risk logic used to perform risk checks and liquidations.
State Variables
TIMELOCK_DURATION
Timelock delay to update asset LTVs
uint256 public constant TIMELOCK_DURATION = 24 * 60 * 60;
TIMELOCK_DEADLINE
Timelock deadline to enforce timely updates
uint256 public constant TIMELOCK_DEADLINE = 24 * 60 * 60;
SENTIMENT_POOL_KEY
Sentiment Pool registry key hash
keccak(SENTIMENT_POOL_KEY)
bytes32 public constant SENTIMENT_POOL_KEY = 0x1a99cbf6006db18a0e08427ff11db78f3ea1054bc5b9d48122aae8d206c09728;
SENTIMENT_RISK_MODULE_KEY
Sentiment Risk Module registry key hash
keccak(SENTIMENT_RISK_MODULE_KEY)
bytes32 public constant SENTIMENT_RISK_MODULE_KEY = 0x881469d14b8443f6c918bdd0a641e9d7cae2592dc28a4f922a2c4d7ca3d19c77;
minLtv
Minimum LTV bound
uint256 public minLtv;
maxLtv
Maximum LTV bound
uint256 public maxLtv;
REGISTRY
Sentiment Registry
Registry public immutable REGISTRY;
pool
Sentiment Singleton Pool
Pool public pool;
riskModule
Sentiment Risk Module
RiskModule public riskModule;
### ltvFor
Fetch the ltv for a given asset in a pool
```solidity
mapping(uint256 poolId => mapping(address asset => uint256 ltv)) public ltvFor;
ltvUpdateFor
Fetch pending LTV update details for a given pool and asset pair, if any
mapping(uint256 poolId => mapping(address asset => LtvUpdate ltvUpdate)) public ltvUpdateFor;
Functions
constructor
constructor(address registry_, uint256 minLtv_, uint256 maxLtv_) Ownable();
Parameters
Name | Type | Description |
---|---|---|
registry_ | address | Sentiment Registry |
minLtv_ | uint256 | Minimum LTV bound |
maxLtv_ | uint256 | Maximum LTV bound |
updateFromRegistry
Fetch and update module addreses from the registry
function updateFromRegistry() external;
getOracleFor
Fetch oracle address for a given asset
function getOracleFor(address asset) public view returns (address);
isPositionHealthy
Check if the given position is healthy
function isPositionHealthy(address position) external view returns (bool);
validateLiquidation
Valid liquidator data and value of assets seized
function validateLiquidation(
address position,
DebtData[] calldata debtData,
AssetData[] calldata assetData
) external view;
validateBadDebt
function validateBadDebt(address position) external view;
getRiskData
Fetch risk-associated data for a given position
function getRiskData(address position) external view returns (uint256, uint256, uint256);
Parameters
Name | Type | Description |
---|---|---|
position | address | The address of the position to get the risk data for |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | totalAssetValue The total asset value of the position |
<none> | uint256 | totalDebtValue The total debt value of the position |
<none> | uint256 | minReqAssetValue The minimum required asset value for the position to be healthy |
getTotalAssetValue
function getTotalAssetValue(address position) external view returns (uint256);
getTotalDebtValue
function getTotalDebtValue(address position) external view returns (uint256);
requestLtvUpdate
Propose an LTV update for a given Pool-Asset pair
overwrites any pending or expired updates
function requestLtvUpdate(uint256 poolId, address asset, uint256 ltv) external;
acceptLtvUpdate
Apply a pending LTV update
function acceptLtvUpdate(uint256 poolId, address asset) external;
rejectLtvUpdate
Reject a pending LTV update
function rejectLtvUpdate(uint256 poolId, address asset) external;
setLtvBounds
Set Protocol LTV bounds
function setLtvBounds(uint256 _minLtv, uint256 _maxLtv) external onlyOwner;
setRiskModule
Set the risk module used to store risk logic for positions
only callable by RiskEngine owner
function setRiskModule(address _riskModule) external onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
_riskModule | address | the address of the risk module implementation |
setOracle
Set the oracle address used to price a given asset
Does not support ERC777s, rebasing and fee-on-transfer tokens
function setOracle(address asset, address oracle) external onlyOwner;
Events
PoolSet
Pool address was updated
event PoolSet(address pool);
RiskModuleSet
Risk Module address was updated
event RiskModuleSet(address riskModule);
LtvBoundsSet
Protocol LTV bounds were updated
event LtvBoundsSet(uint256 minLtv, uint256 maxLtv);
OracleSet
Oracle associated with an asset was updated
event OracleSet(address indexed asset, address oracle);
LtvUpdateRejected
Pending LTV update was rejected
event LtvUpdateRejected(uint256 indexed poolId, address indexed asset);
LtvUpdateAccepted
Pending LTV update was accepted
event LtvUpdateAccepted(uint256 indexed poolId, address indexed asset, uint256 ltv);
LtvUpdateRequested
LTV update was requested
event LtvUpdateRequested(uint256 indexed poolId, address indexed asset, LtvUpdate ltvUpdate);
Errors
RiskEngine_NoOracleFound
There is no oracle associated with the given asset
error RiskEngine_NoOracleFound(address asset);
RiskEngine_LtvLimitBreached
Proposed LTV is outside of protocol LTV bounds
error RiskEngine_LtvLimitBreached(uint256 ltv);
RiskEngine_NoLtvUpdate
There is no pending LTV update for the given Pool-Asset pair
error RiskEngine_NoLtvUpdate(uint256 poolId, address asset);
RiskEngine_OnlyPoolOwner
Function access is restricted to the owner of the pool
error RiskEngine_OnlyPoolOwner(uint256 poolId, address sender);
RiskEngine_LtvUpdateTimelocked
Timelock delay for the pending LTV update has not been completed
error RiskEngine_LtvUpdateTimelocked(uint256 poolId, address asset);
RiskEngine_LtvUpdateExpired
Timelock deadline for LTV update has passed
error RiskEngine_LtvUpdateExpired(uint256 poolId, address asset);
RiskEngine_MinLtvTooLow
Global min ltv cannot be zero
error RiskEngine_MinLtvTooLow();
RiskEngine_MaxLtvTooHigh
Global max ltv must be less than 100%
error RiskEngine_MaxLtvTooHigh();
RiskEngine_CannotBorrowPoolAsset
Pool LTV for the asset being lent out must be zero
error RiskEngine_CannotBorrowPoolAsset(uint256 poolId);
Structs
LtvUpdate
Utility struct to store pending Pool LTV updates
struct LtvUpdate {
uint256 ltv;
uint256 validAfter;
}